package com.zyf.project.capchain.listener.result.template;

import com.zyf.project.capchain.listener.result.impl.Result;
import com.zyf.project.capchain.listener.state.excimpl.CommonExceptionState;
import com.zyf.project.capchain.listener.state.excimpl.ServiceExceptionState;
import com.zyf.project.capchain.listener.state.excimpl.WxExceptionState;
import org.springframework.stereotype.Component;

/**
 * 发生异常时的结果
 *
 * @author Zhou Yifan
 */
@Component
public class ExceptionResultTemplate<E extends Exception> {

    /**
     * 空参数异常
     *
     * @param e Exception
     * @return Result
     */
    public Result<E> emptyParamsResult(E e) {
        Result<E> result = new Result<>();
        result.setResult(CommonExceptionState.EMPTY_PARAMS, e);
        return result;
    }

    /**
     * 数据层错误异常
     *
     * @param e Exception
     * @return Result
     */
    public Result<E> sqlErrorResult(E e) {
        Result<E> result = new Result<>();
        result.setResult(CommonExceptionState.SQL_ERROR, e);
        return result;
    }

    /**
     * 用户不存在异常
     *
     * @param e Exception
     * @return Result
     */
    public Result<E> serviceUserNotExistResult(E e) {
        Result<E> result = new Result<>();
        result.setResult(ServiceExceptionState.SERVICE_USER_NOT_EXIST, e);
        return result;
    }

    /**
     * 微信登陆openid授权失败异常
     *
     * @param e Exception
     * @return Result
     */
    public Result<E> wxLoginNoOpenidResult(E e) {
        Result<E> result = new Result<>();
        result.setResult(WxExceptionState.WX_LOGIN_NO_OPENID, e);
        return result;
    }

    /**
     * 微信登陆用户不存在异常
     *
     * @param e Exception
     * @return Result
     */
    public Result<E> wxLoginUserNotExistResult(E e) {
        Result<E> result = new Result<>();
        result.setResult(WxExceptionState.WX_LOGIN_USER_NOT_EXIST, e);
        return result;
    }

    /**
     * token 异常
     *
     * @param e Exception
     * @return Result
     */
    public Result<E> wxTokenErrorException(E e) {
        Result<E> result = new Result<>();
        result.setResult(WxExceptionState.TOKEN_ERROR, e);
        return result;
    }

    /**
     * 特殊情况的错误，一般为系统错误等
     *
     * @param message 错误消息
     * @param code    错误代码
     * @param data    错误数据
     * @return 返回结果
     */
    public Result<E> specialErrorException(String message, Integer code, E data) {
        Result<E> result = new Result<>();
        result.setMessage(message);
        result.setCode(code);
        result.setData(data);
        return result;
    }

}
